#!/usr/bin/env python
# -*- coding:utf-8 -*-
import functools

# def user_login_data(f):
#     # 使用 functools.wraps 去装饰内层函数，可以保持当前装饰器去装饰的函数的 __name__ 的值不变
#     @functools.wraps(f)
#     def wrapper(*args, **kwargs):
#         # user_id = session.get("user_id", None)
#         # user = None
#         # if user_id:
#         #     # 尝试查询用户的模型
#         #     try:
#         #         user = User.query.get(user_id)
#         #     except Exception as e:
#         #         current_app.logger.error(e)
#         #
#         # g.user = user
#         return f(*args, **kwargs)
#
#     return wrapper
#
#
# @user_login_data
# def num1():
#     pass
#
#
# @user_login_data
# def num2():
#     pass
#
#
# if __name__ == '__main__':
#     print(num1.__name__)
#     print(num2.__name__)
import datetime
import random
import time

from info import db
from info.models import User


def add_test_users():
    users = []
    now = datetime.datetime.now()
    for num in range(0, 10000):
        try:
            user = User()
            user.nick_name = "%011d" % num
            user.mobile = "%011d" % num
            user.password_hash = "pbkdf2:sha256:50000$SgZPAbEj$a253b9220b7a916e03bf27119d401c48ff4a1c81d7e00644e0aaf6f3a8c55829"
            user.last_login = now - datetime.timedelta(seconds=random.randint(0, 2678400))
            users.append(user)
            print(user.mobile)
        except Exception as e:
            print(e)
    from manage import app
    with app.app_context():
        db.session.add_all(users)
        db.session.commit()
    print("ok")


def test_time1():
    # datetime.datetime.now()
    # datetime.datetime.now().strftime()
    # time.localtime()

    print(datetime.datetime.now())
    print(datetime.datetime.now().strftime("%Y-%m-%d"))
    print(datetime.datetime.now().strftime("%Y"))
    print(datetime.datetime.now().strftime("%m"))
    print(datetime.datetime.now().strftime("%d"))

    print("*" * 50)
    print(datetime.datetime.now().year)
    print(datetime.datetime.now().month)
    print(datetime.datetime.now().day)

    print("*" * 50)
    # print(type(datetime.timedelta(days=1)))
    # print(datetime.timedelta(days=1))
    print(time.localtime())
    print(time.localtime().tm_year)
    print(time.localtime().tm_mon)
    print(time.localtime().tm_mday)


def test_time2():
    # 获取上个月最后一天的日期

    # 上一个月的第一天
    lst_fist = datetime.date(datetime.date.today().year, datetime.date.today().month - 1, 1)
    # 上一个月的最后一天
    lst_last = datetime.date(datetime.date.today().year, datetime.date.today().month, 1) - datetime.timedelta(1)
    print(lst_fist)
    print(lst_last)


def test_time3():
    # 当期日期
    # 当前日期的前一天
    print(datetime.datetime.now() - datetime.timedelta(days=1))


if __name__ == '__main__':
    # add_test_users()
    # test_time1()
    # test_time2()
    test_time3()

